import { Commit } from 'vuex'
import { getFriendList } from '../../api/friend'

type FriendLabel = {
  userId: number
  nickname: string
  avatar: string
}

interface FriendState {
  friendList: FriendLabel[] | null
  hasFriendNotification: boolean
}

// friend.module.ts
const friendModule = {
  state: (): FriendState => ({
    friendList: null,
    hasFriendNotification: false
  }),
  mutations: {
    SET_FRIEND_LIST(state: FriendState, friendList: FriendLabel[]) {
      state.friendList = friendList
    },
    SET_FRIEND_NOTIFICATION(state: FriendState, notification: boolean) {
      state.hasFriendNotification = notification
    }
  },
  actions: {
    async GetFriendList({ commit }: { commit: Commit }, args: [userId: number, searchContent: string | null]) {
      try {
        const res = await getFriendList(args[0], args[1])
        if (res.code === 200) {
          commit('SET_FRIEND_LIST', res.data)
        }
      } catch (error) {
        throw error
      }
    }
  },
  getters: {
    getFriendList: (state: FriendState) => state.friendList,
    getFriendNotification: (state: FriendState) => state.hasFriendNotification
  }
}

export default friendModule
